
import {
    INIT_CART_LIST,
    DEL_ONE_SHOP,
    ONE_SHOP_DEAL,
    IS_CHECKED_ALL,
    ONE_SHOP_CHECKED
} from './mutations-type'

import Vue from 'vue';

export default {
    // 1 初始化购物车数据
    [INIT_CART_LIST](state,{shopList}){
        state.shopListArr = shopList;
    },

    // 2 删除一个商品
    [DEL_ONE_SHOP](state,{index}){
        state.shopListArr.splice(index,1);
    },

    // 3 单个商品加减
    [ONE_SHOP_DEAL](state,{shop,flag}){
        state.shopListArr.forEach((value)=>{
            if(value.shopId === shop.shopId){
                if(flag){
                    value.shopNumber +=1;
                  }else{
                    if(value.shopNumber <= 1){
                        value.shopNumber = 1;
                        alert('只有一件商品，不能删除')
                    }else{
                        value.shopNumber -=1;
                    } 
                  }
            }
        })
    },

    // 4 全选和取消全选
    [IS_CHECKED_ALL](state,{flag}){
        state.shopListArr.forEach((value)=>{
          // 4.1 判断 
          if(typeof value.checked === 'undefined'){
            Vue.set(value,'checked',!flag);
          }else{
             value.checked =  !flag;
          }
        })
    },

    // 5 单个商品选择和取消
    [ONE_SHOP_CHECKED](state,{shop}){
        state.shopListArr.forEach((value)=>{
            if(value.shopId === shop.shopId){
                if(typeof shop.checked === 'undefined'){
                    Vue.set(value,'checked',true);
                }else{
                    value.checked =  !value.checked;
                }
            }    
        })
    }

}